// ///////////////////////////// MIT License //////////////////////////////////// //
//                                                                                //
// Copyright (c) 2010 David Zsolt Manrique                                        //
//                    david.zsolt.manrique@gmail.com                              //
//                                                                                //
// Permission is hereby granted, free of charge, to any person obtaining a copy   //
// of this software and associated documentation files (the "Software"), to deal  //
// in the Software without restriction, including without limitation the rights   //
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell      //
// copies of the Software, and to permit persons to whom the Software is          //
// furnished to do so, subject to the following conditions:                       //
//                                                                                //
// The above copyright notice and this permission notice shall be included in     //
// all copies or substantial portions of the Software.                            //
//                                                                                //
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR     //
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,       //
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE    //
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER         //
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,  //
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN      //
// THE SOFTWARE.                                                                  //
//                                                                                //
// ////////////////////////////////////////////////////////////////////////////// //

// standard headers
/*
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <iomanip>
#include <cmath>
#include <cstdarg>
#include <stdexcept>
#include <utility>
#include <tuple>

#include <cmdioutil.h>*/
#include "param_interpret.h"

bool greater_xyz(const Eigen::Vector3d& a,const Eigen::Vector3d& b,double tiny = 1e-12)
{ 
    if(std::abs(a.x()-b.x())<tiny) {
        if(std::abs(a.y()-b.y())<tiny) { if(a.z()>b.z()) return true;  else return false; }
        if(a.y()>b.y()) return true;
        return false;
    }
    if(a.x()>b.x()) return true;
    return false;
};

bool greater_yzx(const Eigen::Vector3d& a,const Eigen::Vector3d& b,double tiny = 1e-12)
{ 
    if(std::abs(a.y()-b.y())<tiny) {
        if(std::abs(a.z()-b.z())<tiny) { if(a.x()>b.x()) return true; else return false; }
        if(a.z()>b.z()) return true;
        return false;
    }
    if(a.y()>b.y()) return true;
    return false;
};

bool greater_zxy(const Eigen::Vector3d& a,const Eigen::Vector3d& b,double tiny = 1e-12)
{ 
    if(std::abs(a.z()-b.z())<tiny) {
        if(std::abs(a.x()-b.x())<tiny) { if(a.y()>b.y()) return true; else return false; }
        if(a.x()>b.x()) return true; 
        return false;
    }
    if(a.z()>b.z()) return true;
    return false;
};


int main(int argc, char** argv)
{
try
{   
	cmd::arguments arg(argc,argv);
	cmd::switcharg<false> help(arg,"-h","--help", "print help message");
        cmd::switcharg<false> stdio(arg,"-std","--std", "standard input and output");
        cmd::switcharg<false> print_out(arg,"-p","--print", "print out result (and no overwrite)");

        xyz c;
        if(*stdio) load(std::cin,c);

        cmd::vararg<std::string> output_fn (arg,"-o","--output","ouput file name","output");
	cmd::multivararg<std::string> selected_range (arg,"-s","--select","(repeatable opt) select atoms, by default all selected");
        cmd::switcharg<false> invert_selection(arg,"-i","--invert-selection", "invert selection range");
        //////////////////////////////////////////////////////////////////////////


        cmd::switcharg<false> sox (arg,"-sox","--sox","sort selected atoms along x axis(in x: along y, in y along z)");
        cmd::switcharg<false> soy (arg,"-soy","--soy","sort selected atoms along y axis(in y: along z, in z along x)");
        cmd::switcharg<false> soz (arg,"-soz","--soz","sort selected atoms along z axis(in z: along x, in x along y)");
        cmd::vararg<double> tiny (arg,"-ti","--tiny","tiny number for coordinate equallity",1.0e-9);

//        cmd::switcharg<false> reverse (arg,"-r","--reverse","reverse order");

        
        ///////////////////////////////////////////////////////////////////////////
        std::string fn;
        if(*help || !*stdio)
        {
	        cmd::posarg<std::string> io_fn(arg,"input file name", "");
                if(!*help) { fn = *io_fn; load(fn,c); }
        }
	
        
	if(*help)
	{
		arg.print_help(std::cout,"xyzsort");
                std::cout << std::endl;
                std::cout << interpret_expression_help();                
                std::cout << std::endl;
                std::cout << "examples:" << std::endl;
//                std::cout << "   xyzrot m.xyz -rao 0/0/1 75.0          #rotate around z axis by 75deg"<< std::endl;
//                std::cout << "   xyzrot m.xyz -ra 0/0/1 75.0 1/1/1     #rotate around z dir. by 75deg and 1/1/1 is the origin"<< std::endl;
//                std::cout << "   xyzrot m.xyz -s 1:5,8 -ra 0/0/1 75.0  #rotate 1..5,8 atoms around z axis by 75deg"<< std::endl;
//                std::cout << "   xyzrot m.xyz -ra 0/0/1 75.0 2         #rotate around z dir. by 75deg and atom 2 is the origin"<< std::endl;
//                std::cout << "   xyzrot m.xyz -r2 3 x 0/2/1            #rotate atom 3 into x axis and 0/2/1 is the origin"<< std::endl;
                std::cout << std::endl;
                std::cout << "By default m.xyz will be overwritten!"<< std::endl;
                std::cout << std::endl;
                std::cout << "Version 0.9b"<< std::endl;
                std::cout << "Copyright (c) 2010 David Zsolt Manrique (david.zsolt.manrique@gmail.com)"<< std::endl;
    		return EXIT_SUCCESS;
	}
        ///////////////////////////////////////////////////////////////////////////
	if(~selected_range) for(int i=0;i<(*selected_range).size();i++) select_by_expression(c,(*selected_range)[i]); else neg(c);
        if(*invert_selection) neg(c);
        ////////////////////////////////////////////////////
  
        Eigen::Vector3d a1,a2,a3,na1,na2,na3;
        a1 = c.lattice[0];
        a2 = c.lattice[1];
        a3 = c.lattice[2];
        na1 = a1.normalized();
        na2 = a2.normalized();
        na3 = a3.normalized();
        
        if(~sox) sort(c,greater_xyz,*tiny);
        else if(~soy) sort(c,greater_yzx,*tiny);
        else if(~soz) sort(c,greater_zxy,*tiny);
        
//        if(~reverse) reverse_order(c);
        
        
        
        ////////////////////////////////////////////////////
        if(*print_out) print(std::cout,c);
        else if(*stdio) save(std::cout,c);
        else if(~output_fn) save(*output_fn,c);
        else save(fn,c);

} catch(const char* message)
{
	std::cerr << message << std::endl;
	std::cerr << "(exit)" << std::endl;
	return EXIT_FAILURE;
} 
catch(const std::exception & e)
{
        std::cerr << e.what() << std::endl;
        std::cerr << "(exit)" << std::endl;
        return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}


